package com.example.power;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public  class MainActivity extends Activity {

    private TextView tvBatteryChanged;

    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvBatteryChanged = (TextView)findViewById(R.id.tvBatteryChanged);
        //注册一个接受广播类型
        registerReceiver(batteryChangedReceiver,  new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    //接受广播
    private BroadcastReceiver batteryChangedReceiver =  new BroadcastReceiver() {

        public  void onReceive(Context context, Intent intent) {
            if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
                int level = intent.getIntExtra( "level", 0);
                int scale = intent.getIntExtra( "scale", 100);
                tvBatteryChanged.setText( "电池电量：" + (level * 100 / scale) +  "%");
            }
        }
    };
}